package com.zhangye.test.controller;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zhangye.test.util.StringUtils;

@Controller
@RequestMapping("common")
public class CommonController extends BaseController{
	
	
	/**
	 * 发送验证码
	 * 作者： 张叶
	 * 创建时间：2018年5月28日  下午4:05:04
	 */
	@ResponseBody
	@RequestMapping(value = "/captcha", method = RequestMethod.GET)
	public String validatedemo(String mobile,String code,HttpServletRequest request,HttpServletResponse response){
		int i = ResultServlet.validate(request, response, code);
	    JSONObject result = new JSONObject();
		if(i == 0){
			if (StringUtils.isEmpty(mobile)) {
				return null;
		    }
			// 3、发送验证码
			int reInt;
			try {
				if(isNumLegal(mobile)){
//					reInt = smsService.sendMessage(mobile);
					reInt = sendMsgService.sendMsg(mobile, 0);
					if (reInt != 1) {
					    result.put("status", "1");//系统错误发送失败
					} else {
						result.put("status", "发送成功");
					}
				}else{
					result.put("status", "3");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			result.put("status", "4");
		}
	    return null;
	}
	
    private static boolean isNumLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
